package com.superteam.zzl.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.superteam.zzl.activity.R;
import com.superteam.zzl.activity.databinding.ActivityInfoFootBinding;
import com.superteam.zzl.activity.databinding.ActivityInfoItemivBinding;
import com.superteam.zzl.activity.databinding.ActivityInfoItemlvBinding;
import com.superteam.zzl.activity.databinding.ActivityInfoItemvpBinding;
import com.superteam.zzl.model.DynamicFragment.bean.Group.DataBean;
import com.superteam.zzl.model.DynamicFragment.bean.Info.DataBean.SellerBean;
import com.superteam.zzl.model.DynamicFragment.bean.Info.DataBean.TrendsBean;

import java.util.List;

/**
 * Created by Administrator on 2017/1/14.
 */

public class InfoActivityAdapter extends RecyclerViewBaseAdapter<Object, InfoActivityAdapter.ViewHolder> {


    public InfoActivityAdapter(Context context, List<Object> list) {
        super(context, list);
    }

    @Override
    public int getItemCount() {
        if (list == null || list.size() == 0) {
            return 0;
        } else {
            return list.size() + 1;
        }
    }

    @Override
    public int getItemViewType(int position) {
        Object o = list.get(position);
        if (o instanceof TrendsBean) {
            return 0;
        } else if (o instanceof SellerBean) {
            return 2;
        } else if (position != list.size() - 1){
            return 1;
        } else {
            return 3;
        }

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType) {
            case 0:
                view = inflater.inflate(R.layout.activity_info_itemlv, parent, false);
                break;
            case 1:
                view = inflater.inflate(R.layout.activity_info_itemvp, parent, false);
                break;
            case 2:
                view = inflater.inflate(R.layout.activity_info_itemiv, parent, false);
                break;
            case 3:
                view = inflater.inflate(R.layout.activity_info_foot, parent, false);
                break;

            default:
                break;
        }

        return new ViewHolder(view, viewType);
    }


    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        int viewType = getItemViewType(position);
        if (viewType != holder.type) {
            holder = onCreateViewHolder(getRecyclerView(), viewType);
        }

        switch (viewType) {
            case 0:
                ActivityInfoItemlvBinding lvBinding = holder.getLvBinding();
                TrendsBean trends = getData(TrendsBean.class);
                lvBinding.setTrends(trends);
                Glide.with(context).load(trends.cover_image).into(lvBinding.iv);
                lvBinding.executePendingBindings();
                break;
            case 1:
                ActivityInfoItemvpBinding vpBinding = holder.getVpBinding();
                DataBean data = getData(DataBean.class);
                vpBinding.vp.setAdapter(new InfoActivityVpAdapter(context, data.group_list));
                vpBinding.executePendingBindings();
                break;
            case 2:
                ActivityInfoItemivBinding ivBinding = holder.getIvBinding();
                SellerBean seller = getData(SellerBean.class);
                ivBinding.setSeller(seller);
                Glide.with(context).load(seller.cover_image).skipMemoryCache(true).into(ivBinding.iv);
                ivBinding.executePendingBindings();
                break;
            case 3:
//                ActivityInfoFootBinding footBinding = holder.getFootBinding();
                break;

            default:
                break;
        }

    }

    private <T> T getData(Class<T> t) {
        for (int i = 0; i < list.size(); i++) {
            Object o = list.get(i);
            if (o.getClass() == t) {
                return (T) o;
            }
        }
        return null;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private ActivityInfoItemlvBinding lvBinding;
        private ActivityInfoItemvpBinding vpBinding;
        private ActivityInfoItemivBinding ivBinding;
        private ActivityInfoFootBinding footBinding;
        private int type;

        public ViewHolder(View itemView, int viewType) {
            super(itemView);
            type = viewType;
            initBinding(itemView, viewType);
        }

        private void initBinding(View itemView, int viewType) {

            switch (viewType) {
                case 0:
                    lvBinding = DataBindingUtil.bind(itemView);
                    break;
                case 1:
                    vpBinding = DataBindingUtil.bind(itemView);
                    break;
                case 2:
                    ivBinding = DataBindingUtil.bind(itemView);
                    break;
                case 3:
                    footBinding = DataBindingUtil.bind(itemView);
                    break;

                default:
                    break;
            }
        }


        public ActivityInfoItemlvBinding getLvBinding() {
            return lvBinding;
        }

        public ActivityInfoItemvpBinding getVpBinding() {
            return vpBinding;
        }

        public ActivityInfoItemivBinding getIvBinding() {
            return ivBinding;
        }

        public ActivityInfoFootBinding getFootBinding() {
            return footBinding;
        }


    }



}
